﻿using System;
using System.Web;
using System.Text;
using System.Data.SQLite;

namespace Com.Toook.Common
{
    /// <summary>
    /// Sqlite辅助类
    /// </summary>
    public class SqliteHelper
    {
        public SqliteHelper()
        {
        }
        /// <summary>
        /// 获取Sqlite数据库连接对象, 默认devDB.db
        /// </summary>
        /// <returns></returns>
        public static SQLiteConnection GetConnection()
        {
            SQLiteConnection con = new SQLiteConnection(GetConStr());
            return con;
        }
        /// <summary>
        /// 获取dbName指定的Sqlite数据库连接对象.如：SQLiteDatabase
        /// </summary>
        /// <returns></returns>
        public static SQLiteConnection GetConnection(string dbName)
        {
            SQLiteConnection con = new SQLiteConnection(GetConStr(dbName));
            return con;
        }
        /// <summary>
        /// 关闭数据库资源
        /// </summary>
        /// <param name="con">数据库连接，没有传null</param>
        /// <param name="reader">SQLiteDataReader，没有传null</param>
        public static void CloseDbResource(SQLiteConnection con, SQLiteDataReader reader)
        {
            if(reader != null)
            {
                try
                {
                    reader.Close();
                }
                catch (Exception ex)
                {
                    LogHelper.Log.Error("SqliteHelper.CloseDbResource reader 错误", ex);
                }
            }
            if(con != null)
            {
                try
                {
                    con.Close();
                }
                catch (Exception ex)
                {
                    LogHelper.Log.Error("SqliteHelper.CloseDbResource con 错误", ex);
                }
            }
        }
       
        private static string GetConStr()
        {
            if(connectionString == null)
            {
                connectionString = "Data Source=" + HttpContext.Current.Request.PhysicalApplicationPath
                    + "App_Data\\devDB.db;password=nest99MOVIE;Pooling=true;FailIfMissing=false;";
            }
            return connectionString;
        }
        /// <summary>
        /// 动态生成连接字符串
        /// </summary>
        /// <param name="dbName"></param>
        /// <returns></returns>
        private static string GetConStr(string dbName)
        {
            connectionStringDynamic = "Data Source=" + HttpContext.Current.Request.PhysicalApplicationPath
                    + "App_Data\\" + dbName + ".db;password=nest99MOVIE;Pooling=true;FailIfMissing=false;";

            return connectionStringDynamic;
        }       
       
        /// <summary>
        /// Sqlite数据库默认连接字符串
        /// </summary>
        private static string connectionString =
            "Data Source=" + HttpContext.Current.Request.PhysicalApplicationPath
          + "App_Data\\devDB.db;password=nest99MOVIE;Pooling=true;FailIfMissing=false;";
        /// <summary>
        /// 根据传入的数据库名称，生成Sqlite数据库连接字符串
        /// </summary>
        private static string connectionStringDynamic = null;
    }
}
